package org.csu.onlineedubackend.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.onlineedubackend.entity.Question;
import org.csu.onlineedubackend.mapper.QuestionMapper;
import org.csu.onlineedubackend.service.QuestionService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    @Override
    public List<Question> getQuestionsByCourseId(Integer courseId) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Question createQuestion(Question question) {
        // 可以在这里添加一些业务逻辑，比如检查参数等
        baseMapper.insert(question);
        return question;
    }

    @Override
    public Question updateQuestion(Question question) {
        System.out.println("QuestionServiceImpl.updateQuestion - 输入的question: " + question);
        System.out.println("QuestionServiceImpl.updateQuestion - resourceUrls: " + question.getResourceUrls());
        
        // 检查题目是否存在
        Question existingQuestion = baseMapper.selectById(question.getId());
        if (existingQuestion == null) {
            throw new RuntimeException("题目不存在");
        }
        
        System.out.println("QuestionServiceImpl.updateQuestion - 更新前的existingQuestion: " + existingQuestion);
        System.out.println("QuestionServiceImpl.updateQuestion - 更新前的resourceUrls: " + existingQuestion.getResourceUrls());
        
        // 确保resourceUrls字段被正确设置
        if (question.getResourceUrls() != null) {
            System.out.println("QuestionServiceImpl.updateQuestion - 设置resourceUrls: " + question.getResourceUrls());
        }
        
        // 更新题目
        int updateResult = baseMapper.updateById(question);
        System.out.println("QuestionServiceImpl.updateQuestion - 更新结果: " + updateResult);
        
        // 如果更新成功，再次查询确认
        if (updateResult > 0) {
            Question updatedQuestion = baseMapper.selectById(question.getId());
            System.out.println("QuestionServiceImpl.updateQuestion - 更新后的question: " + updatedQuestion);
            System.out.println("QuestionServiceImpl.updateQuestion - 更新后的resourceUrls: " + updatedQuestion.getResourceUrls());
            return updatedQuestion;
        } else {
            System.out.println("QuestionServiceImpl.updateQuestion - 更新失败");
            throw new RuntimeException("更新作业失败");
        }
    }

    @Override
    public boolean deleteQuestion(Integer questionId) {
        // 检查题目是否存在
        Question existingQuestion = baseMapper.selectById(questionId);
        if (existingQuestion == null) {
            return false;
        }
        
        // 删除题目
        int result = baseMapper.deleteById(questionId);
        return result > 0;
    }
}